Copyright (C) 2020 Andreas Kloeckner
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import numpy as np
import matplotlib.pyplot as pt
import scipy.sparse as sps
COOrdinate format is typically convenient for building ("assembling") a sparse matrix:
data = [5, 6, 7]
rows = [1, 1, 2]
columns = [2, 4, 6]
A = sps.coo_matrix(
(data, (rows, columns)),
shape=(10, 10), dtype=np.float64)
A
A.todense()
A.nnz
pt.spy(A)
For a COO matrix, the juicy attributes are data
, row
, and col
.
print("row:", A.row)
print("col:", A.col)
print("data:", A.data)
COOrdinate format is not the only format.
There is also Compressed Sparse Row:
Acsr = A.tocsr()
Acsr
For Compressed Sparse Row, look in data
, indptr
, and indices
.
print("indptr:", Acsr.indptr)
print("indices:", Acsr.indices)
print("data:", Acsr.data)
The following code randomly generates a sparse matrix that has a given fill_percent
percentage of nonzero entries:
fill_percent = 5
size = 1000
nentries = size**2 * fill_percent // 100
data = np.random.randn(nentries)
rows = (np.random.rand(nentries)*size).astype(np.int32)
columns = (np.random.rand(nentries)*size).astype(np.int32)
B_coo = sps.coo_matrix(
(data, (rows, columns)),
shape=(size, size), dtype=np.float64)
B_csr = sps.csr_matrix(B_coo)
B_dense = B_coo.todense()
Next, we time matrix-vector multiplication for different versions of B
:
vec = np.random.randn(size)
from time import time
start = time()
for i in range(2000):
B_dense.dot(vec)
print("time: %g" % (time() - start))